package code.jam.core;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import code.jam.R;
import code.jam.context.JamConfigContext;

public class JamManager extends java.lang.Object
{
	public static final String		DEBUG_TAG				= "JamManager";

	private static JamManager		_instance				= null;
	private JamBGMManager			bgm_manager				= null;
	private JamInstrumentManager	instrument_manager		= null;
	private JamConfigContext		jam_configure_context	= null;
	
	private List<Bitmap> list = null;

	private boolean is_finish = false;

	private JamManager()
	{
		super();
	}

	public void drop()
	{
		
		if (bgm_manager != null)
		{
			bgm_manager.drop();
		}

		if (instrument_manager != null)
		{
			instrument_manager.dropAll();
		}
		
		if(list != null)
		{
			for(Bitmap bm: list)
			{
//				bm.recycle();
			}
		}
	}
	
	public void setFinish(boolean finish)
	{
		this.is_finish  = finish;
	}
	
	public boolean isFinish()
	{
		return is_finish;
	}

	static public JamManager getInstance()
	{

		if (_instance == null)
		{
			_instance = new JamManager();
		}

		return _instance;
	}

	public JamBGMManager getBGMMangerInstance()
	{
		if (bgm_manager == null)
		{
			bgm_manager = new JamBGMManager();
		}

		return bgm_manager;
	}

	public JamInstrumentManager getInstrumentManager()
	{
		if (instrument_manager == null)
		{
			instrument_manager = new JamInstrumentManager();
		}

		return instrument_manager;
	}

	public void setJamConfigContext(JamConfigContext config_context)
	{
		jam_configure_context = config_context;
	}

	public JamConfigContext getJamConfigContext()
	{
		return jam_configure_context;
	}

	public boolean start()
	{
		String jam_config_string = JamDataManager.loadJamConfigFromHTTP();

		if (jam_config_string.length() == 0)
		{
			Log.e(DEBUG_TAG, "can't connect the network service.. trying to read jam config from sd");

			JamConfigContext config_from_sd = JamDataManager.loadAndParseJamConfigFromSD();

			if (config_from_sd == null)
			{
				Log.e(DEBUG_TAG, "can't road JAM Config File... finishing this application...");
				return false;

			}
			else
			{
				this.jam_configure_context = config_from_sd;
				Log.d(DEBUG_TAG, "jam manager is done...");
			}
		}
		else
		{
			JamDataManager.writeJamConfigAtSD(jam_config_string);

			JamConfigContext config_from_http = JamParser.parseJamConfig(jam_config_string);

			if (config_from_http == null)
			{
				Log.e(DEBUG_TAG, "jam.conf can't parse anyway... jam doesn't start...");
			}
			else
			{			
				this.jam_configure_context = config_from_http;
				Log.d(DEBUG_TAG, "jam manager is done...");
			}
		}

		return true;

	}
	
	public void setJamProgressBitmap(Context context)
	{
		if(list == null)
		{
			list = new ArrayList<Bitmap>(8);
			
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0001));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0002));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0003));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0004));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0005));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0006));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0007));
			list.add(BitmapFactory.decodeResource( context.getResources(), R.drawable.loading_mb_0008));
			
			Log.d(DEBUG_TAG, "loading bitmap is completed");
		}
	}
	
	public List<Bitmap> loadJamProgressBitmap()
	{
		if( list != null)
		{
			return list;
		}
		
		return null;
	}
}
